package com.seashell;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * <ul>
 * <li>文件名称 : com.seashell.ExcelReader
 * </li>
 * <li>版权所有 : 版权所有(C)2016-2020</li>
 * <li>公 司 : CST</li>
 * <li>创建时间 : 2017/1/17</li>
 * <li>修改记录 : </li>
 * </ul>
 * 类说明：
 *
 * @author Allen
 * @version 1.0
 */
public class ExcelReader {

    private File excel;

    public ExcelReader(String excelName){
        excel = new File(excelName);
        if(!excel.exists()){
            throw new RuntimeException("can not find file:" + excelName);
        }
    }

    public List<Map<String,Object>> doRead() throws IOException {
        List<Map<String,Object>> targetList = new ArrayList<>();
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(excel));
        for(Iterator<XSSFSheet> iterator = workbook.iterator();iterator.hasNext();) {
            XSSFSheet next = iterator.next();
            String sheetName = next.getSheetName().trim();
            int rowCounts = next.getLastRowNum();

            for (int i = 1; i < rowCounts+1; i++) {
                XSSFRow row = next.getRow(i);
                if(row!=null){

                    //EGRADE
                    XSSFCell egradeCell = row.getCell(0);
                    if(egradeCell==null){
                        continue;
                    }
                    String egradeValue = egradeCell.getStringCellValue();

                    Map<String, Object> rowMap = getFirstInRow(row,1);
                    if(rowMap!=null){
                        rowMap.put("EGRADE",egradeValue);
                        rowMap.put("SHEET",sheetName);
                        targetList.add(rowMap);
                    }

                    Map<String, Object> rowMap2 = getFirstInRow(row,5);
                    if(rowMap2!=null){
                        rowMap2.put("EGRADE",egradeValue);
                        rowMap2.put("SHEET",sheetName);
                        targetList.add(rowMap2);
                    }
                }
            }
        }
        return targetList;
    }

    private Map<String,Object> getFirstInRow(XSSFRow row,Integer offSet){
        Map<String,Object> rowMap = new HashMap<>();
        XSSFCell cell = row.getCell(offSet + 0);
        XSSFCell enameCell = row.getCell(offSet + 1);
        XSSFCell jobCell = row.getCell(offSet + 2);
        XSSFCell salCell = row.getCell(offSet + 3);
        if(cell==null){
            return null;
        }

        //EMPNO
        rowMap.put("EMPNO",cell.getStringCellValue());

        //ENAME
        rowMap.put("ENAME",enameCell.getStringCellValue());

        //JOB
        rowMap.put("JOB",jobCell.getStringCellValue());

        //SAL
        rowMap.put("SAL",salCell.getStringCellValue());

        return rowMap;
    }
}
